package cloud.estate.car.service.impl;

import cloud.estate.car.service.CarService;
import cloud.estate.commons.bean.Car;
import cloud.estate.commons.handler.exception.*;
import cloud.estate.commons.mapper.CarMapper;
import cloud.estate.commons.util.JsonResult;
import cloud.estate.commons.util.ResultTool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {
    @Resource
    private CarMapper carMapper;
    @Override
    public Car getCarById(String id) {
        return null;
    }
    @Override
    public JsonResult addCar(Car car) {
        try {
            boolean save = save(car);
            if ( save){
                return ResultTool.success("添加成功");
            }
            return ResultTool.fail(500,"添加失败");
        } catch (Exception e) {
            throw new AddOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult updateCar(Car car) {
        try {
            boolean update = updateById(car);
            if (update){
                return ResultTool.success("修改成功");
            }
            return ResultTool.fail(501,"修改失败");
        } catch (Exception e) {
            throw new UpdateOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult deleteCar(String id) {
        try {
            boolean delete = removeById(id);
            if (delete){
                return ResultTool.success("删除成功");
            }
            return ResultTool.fail(502,"删除失败");
        } catch (Exception e) {
            throw new DealOneByIdException(e.getMessage());
        }
    }
    @Override
    public JsonResult queryCar(String id) {
        try {
            Car car = getById(id);
            if (car != null){
                return ResultTool.success(car);
            }
            return ResultTool.fail(503,"查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult queryAllCar() {
        try {
            return ResultTool.success(list());
        } catch (Exception e) {
            throw new FindAllException(e.getMessage());
        }
    }
}
